package com.js.rtu.fragment.third;


import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.js.rtu.R;
import com.js.rtu.RTUApplication;
import com.js.rtu.base.BaseFragment;
import com.js.rtu.config.CmdConfig;
import com.js.rtu.mvp.bean.Cmd02Bean;
import com.js.rtu.mvp.bean.Cmd90Bean;
import com.js.rtu.mvp.bean.Cmd92Bean;
import com.js.rtu.mvp.bean.Cmd92Bean_v2_0_1;
import com.js.rtu.mvp.bean.base.CommonBean;
import com.js.rtu.mvp.present.Cmd02Presenter;
import com.js.rtu.mvp.present.Cmd90Presenter;
import com.js.rtu.mvp.present.Cmd92Presenter;
import com.js.rtu.mvp.present.Cmd92Presenter_v2_0_1;
import com.js.rtu.util.BluetoothUtils;
import com.js.rtu.util.CmdUtils;
import com.js.rtu.util.RxTimerUtil;
import com.js.rtu.util.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by YoKeyword on 16/6/30.
 */
public class Pager4Fragment extends BaseFragment {


    @BindView(R.id.cb_auto)
    CheckBox cbAuto;

    RxTimerUtil rxTimerUtil;


    public static Pager4Fragment newInstance() {
        Bundle args = new Bundle();
        Pager4Fragment fragment = new Pager4Fragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_pager4;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        loadRootFragment(R.id.fl_container, DebugViewPagerFragment.newInstance());//22_07设置终端工作参数
        rxTimerUtil = new RxTimerUtil();
        Heartbeat();
    }


    @OnClick({R.id.tv_cmd92, R.id.tv_cmd90, R.id.tv_cmd08, R.id.tv_cmd01})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cmd92:
//                String msg="EE EE 32 34 30 31 30 30 30 31 A2 01 92 01 3C 00 02 01 03 05 05 AA 06 B4 03 07 55 09 32 34 30 31 30 30 30 31 10 31 33 38 32 34 30 31 30 30 30 31 13 43 4D 4E 45 54 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 64 6D 70 2D 6D 71 74 74 2E 63 75 69 6F 74 2E 63 6E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 15 55 73 65 72 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 50 61 73 73 77 6F 72 64 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 17 01 02 0F 01 01 01 04 08 00 00 00 00 00 01 06 12 84 03 02 00 01 04 08 00 00 00 00 00 02 06 12 84 03 18 02 20 01 00 00 00 00 3F 00 00 20 40 04 00 00 16 44 00 00 00 00 00 00 80 3F 00 00 00 00 F4 01 00 00 AA 02 00 00 00 00 3F 00 00 20 40 04 00 00 16 44 00 00 00 00 00 00 80 3F 00 00 00 00 F4 01 00 00 AA 19 02 01 00 01 03 02 00 01 03 20 00 21 AA 3C 00 AA 3C 00 23 18 03 15 10 27 32 24 02 99 23 77 47 5B 07 05 AA DA 13 BE F4 8F 17 02 55 30 63 75 31 73 68 33 67 36 78 73 6E 79 75 39 64 74 63 75 31 73 68 33 67 36 78 73 6E 79 75 39 64 74 85 64 FF FF";
//                Cmd92Presenter_v2_0_1 presenter = new Cmd92Presenter_v2_0_1();
//                CommonBean<Cmd92Bean_v2_0_1> bean = presenter.paseData(msg);
//                presenter.setInfo(bean);
                get92Info();
                break;
            case R.id.tv_cmd90:
//                String msg="EE EE 32 34 30 31 30 30 30 31 A9 00 90 0E 00 00 0D 30 0F 19 49 F6 62 40 10 5C 03 00 00 11 00 00 00 00 12 64 03 00 00 13 00 00 00 00 14 00 00 00 00 15 3C 03 00 00 16 4E 38 2E 36 2E 32 31 00 17 AD 21 00 00 18 52 54 55 2D 42 31 30 5F 59 4C 4E 2D 44 4D 50 00 19 00 00 00 00 20 64 03 00 00 21 00 00 00 00 22 5C 03 00 00 23 00 00 00 00 24 59 03 00 00 25 01 00 00 00 26 18 03 1B 0A 21 25 27 C9 00 28 59 03 00 00 29 04 22 05 17 02 7E 30 A7 DB DA 57 E8 14 03 00 31 00 00 00 00 00 00 00 00 32 48 28 0F 7F 0E ED 9B 08 54 9A FF FF";
//
//                Cmd90Presenter presenter = new Cmd90Presenter();
//                CommonBean<Cmd90Bean> bean = presenter.paseData(msg);
//                presenter.setInfo(bean);
//                dismissLoadingDialog();
                get90Info();
                break;
            case R.id.tv_cmd08:
                //                get08Info(tvInfo);
                get02Info();
                break;
            case R.id.tv_cmd01:
                get01Info();
                break;
        }
    }

    public void get02Info() {
        showLoadingDialog();
        String cmd = CmdUtils.getCmd(CmdConfig.CMD_02);
        BluetoothUtils.send(cmd, new BluetoothUtils.onCallback() {
            @Override
            public void onReceive(String msg) {
                EventBus.getDefault().post(new DebugReadBean(msg));
                Cmd02Presenter presenter = new Cmd02Presenter();
                CommonBean<Cmd02Bean> bean = presenter.paseData(msg);
                presenter.setInfo(bean);
                dismissLoadingDialog();
            }

            @Override
            public void onFail(String err) {
                dismissLoadingDialog();
                toastShow(err);
            }
        });

//        String msg="EE EE 30 30 30 30 39 36 33 39 76 00 02 15 0C 0E 09 14 29 0C 00 0F 12 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 0A 07 00 00 00 00 00 00 00 00 00 00 28 0A 73 06 0D 02 03 0B 00 0E 00 64 F0 FF FF ";
////        String msg="EE EE 32 33 31 31 30 30 30 33 3F 00 02 17 0C 06 0D 33 0E 0C 00 0F 08 5C 4F D6 41 47 53 F0 40 40 54 89 42 E0 79 AF 42 00 A5 87 40 62 18 6E 3B 00 00 7A 3D 52 4F AF BE 0A 00 0D 00 0B 00 0E 00 36 47 FF FF";
//
//        Cmd02Presenter presenter = new Cmd02Presenter();
//        CommonBean<Cmd02Bean> bean = presenter.paseData(msg);
//        presenter.setInfo(bean);
    }

    public void get01Info() {
        //        showLoadingDialog();
        String cmd = CmdUtils.getCmd(CmdConfig.CMD_01);
        BluetoothUtils.send(cmd, new BluetoothUtils.onCallback() {
            @Override
            public void onReceive(String msg) {
                //                dismissLoadingDialog();
                EventBus.getDefault().post(new DebugReadBean(msg));
            }

            @Override
            public void onFail(String err) {
                //                dismissLoadingDialog();
                toastShow(err);
            }
        });
    }


    private void Heartbeat() {
        cbAuto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    startHeartbeat();
                } else {
                    rxTimerUtil.cancel();
                }
            }
        });
    }


    /**
     * 心跳,发送校时命名
     */
    private void startHeartbeat() {
        int TIME_BEAT = (int) SPUtils.get(_mActivity, "time_beat", 20);
        rxTimerUtil.interval(5* 1000, new RxTimerUtil.IRxNext() {
            @Override
            public void doNext(long number) {
                String data = new SimpleDateFormat("yy MM dd HH mm ss", Locale.CHINA).format(new Date(System.currentTimeMillis()));
                String[] str = data.split(" ");
                StringBuilder stringBuilder = new StringBuilder();
                for (String s : str) {
                    stringBuilder.append(String.format("%02x", Integer.parseInt(s))).append(" ");
                }

                //当在N秒的区间没发送过指令才需要发送心跳保持连接
                if (RTUApplication.lastHeartbeatTime != 0) {
                    long now = new Date(System.currentTimeMillis()).getTime();
                    if (now - RTUApplication.lastHeartbeatTime <= TIME_BEAT * 1000.0f-5000.0f) {
                        //15S
                        return;
                    }
                }
                RTUApplication.lastHeartbeatTime = new Date(System.currentTimeMillis()).getTime();


                String cmd = CmdUtils.getCmd(CmdConfig.CMD_21, stringBuilder.substring(0, stringBuilder.length() - 1));
                BluetoothUtils.send(cmd, new BluetoothUtils.onCallback() {
                    @Override
                    public void onReceive(String msg) {
//                        Cmd11Presenter presenter = new Cmd11Presenter();
//                        CommonBean<String> receiveData = presenter.paseData(msg);
//                        EventBus.getDefault().post(new DebugReadBean(String.format("终端时间：%s", receiveData.getData())));

                    }

                    @Override
                    public void onFail(String err) {

                    }
                });
            }
        });


        long lastTime=0;

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void connect(String msg) {
        if (msg.equals("已连接")) {
            cbAuto.setChecked(true);
        } else {
            cbAuto.setChecked(false);
        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
